package com.kanesoft.android.gdx.actors.fight;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.kanesoft.android.gdx.control.input.TouchEventsCallBack;
import com.kanesoft.android.gdx.control.input.TouchGesturesController;
import com.kanesoft.android.gdx.control.input.TouchVectors;

public class ControlActorFightTouch extends InputAdapter implements TouchEventsCallBack {

    private FightActorObject controlledActor;
    private TouchGesturesController gestureCtrl;

    public ControlActorFightTouch(FightActorObject controlledActor) {
        this.controlledActor = controlledActor;
        gestureCtrl = new TouchGesturesController(this);
        initializeGestures();
    }

    public void drawTouchArrows(float deltaTime) {
        gestureCtrl.drawArrows(deltaTime);
    }

    private enum GesturesActions {
         MOVE_FIREBALL_RIGHT, MOVE_FIREBALL_LEFT, DO_PUNCHS, DO_KICKS, DO_JUMP_FORWARD, DO_JUMP_BACKWARD, DO_JUMP_UP
    };

    private final float MINIMUM_INTENSITY = 30f;

    public void initializeGestures() {
        addPowers();
        addPunchsHandler();
        addKicksHandler();
        addJumps();
    }
    
    private void addPowers() {

        TouchVectors[] doFireBall = {
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_180, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_45, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_0, MINIMUM_INTENSITY) };
        gestureCtrl.addGestureHandler(doFireBall, GesturesActions.MOVE_FIREBALL_RIGHT);

        TouchVectors[] doFireBall2 = {
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_180, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_45, MINIMUM_INTENSITY) };
        gestureCtrl.addGestureHandler(doFireBall2, GesturesActions.MOVE_FIREBALL_RIGHT);

        TouchVectors[] doFireBall3 = {
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_180, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_90, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_45, MINIMUM_INTENSITY) };
        gestureCtrl.addGestureHandler(doFireBall3, GesturesActions.MOVE_FIREBALL_RIGHT);

        TouchVectors[] doFireBall4 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_0, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_135, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_180, MINIMUM_INTENSITY) };
        gestureCtrl.addGestureHandler(doFireBall4, GesturesActions.MOVE_FIREBALL_LEFT);

        TouchVectors[] doFireBall5 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_0, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_135, MINIMUM_INTENSITY) };
        gestureCtrl.addGestureHandler(doFireBall5, GesturesActions.MOVE_FIREBALL_LEFT);

        TouchVectors[] doFireBall6 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_0, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_90, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_135, MINIMUM_INTENSITY) };
        gestureCtrl.addGestureHandler(doFireBall6, GesturesActions.MOVE_FIREBALL_LEFT);

    }
    
    private void addJumps() {
        gestureCtrl.addGestureHandler(new TouchVectors(TouchGesturesController.GestureAngle.DEG_45, MINIMUM_INTENSITY),
        GesturesActions.DO_JUMP_FORWARD);
        gestureCtrl.addGestureHandler(new TouchVectors(TouchGesturesController.GestureAngle.DEG_90, MINIMUM_INTENSITY),
        GesturesActions.DO_JUMP_UP);
        gestureCtrl.addGestureHandler(new TouchVectors(TouchGesturesController.GestureAngle.DEG_135, MINIMUM_INTENSITY),
        GesturesActions.DO_JUMP_BACKWARD);
    }
    
    
    private void addKicksHandler() {
        TouchVectors[] doKicks01 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_225, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_45, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_225, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_45, MINIMUM_INTENSITY), };
       
        TouchVectors[] doKicks02 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_225, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_45, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_225, MINIMUM_INTENSITY) };
        
        TouchVectors[] doKicks03 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_225, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_45, MINIMUM_INTENSITY) };
        
        TouchVectors[] doKicks04 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_315, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_135, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_315, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_135, MINIMUM_INTENSITY), };
       
        TouchVectors[] doKicks05 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_315, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_135, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_315, MINIMUM_INTENSITY) };
        
        TouchVectors[] doKicks06 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_315, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_135, MINIMUM_INTENSITY) };
        
        
        gestureCtrl.addGestureHandler(doKicks01, GesturesActions.DO_KICKS);
        gestureCtrl.addGestureHandler(doKicks02, GesturesActions.DO_KICKS);
        gestureCtrl.addGestureHandler(doKicks03, GesturesActions.DO_KICKS);
        gestureCtrl.addGestureHandler(doKicks04, GesturesActions.DO_KICKS);
        gestureCtrl.addGestureHandler(doKicks05, GesturesActions.DO_KICKS);
        gestureCtrl.addGestureHandler(doKicks06, GesturesActions.DO_KICKS);
    }
    
    
    
    private void addPunchsHandler() {
        TouchVectors[] doPunchs01 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_0, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_180, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_0, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_180, MINIMUM_INTENSITY), };
       
        TouchVectors[] doPunchs02 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_0, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_180, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_0, MINIMUM_INTENSITY) };
        
        TouchVectors[] doPunchs03 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_0, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_180, MINIMUM_INTENSITY) };
        
        TouchVectors[] doPunchs04 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_180, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_0, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_180, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_0, MINIMUM_INTENSITY), };
       
        TouchVectors[] doPunchs05 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_180, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_0, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_180, MINIMUM_INTENSITY) };
        
        TouchVectors[] doPunchs06 = { new TouchVectors(TouchGesturesController.GestureAngle.DEG_180, MINIMUM_INTENSITY),
                new TouchVectors(TouchGesturesController.GestureAngle.DEG_0, MINIMUM_INTENSITY) };
        
        gestureCtrl.addGestureHandler(doPunchs01, GesturesActions.DO_PUNCHS);
        gestureCtrl.addGestureHandler(doPunchs02, GesturesActions.DO_PUNCHS);
        gestureCtrl.addGestureHandler(doPunchs03, GesturesActions.DO_PUNCHS);
        gestureCtrl.addGestureHandler(doPunchs04, GesturesActions.DO_PUNCHS);
        gestureCtrl.addGestureHandler(doPunchs05, GesturesActions.DO_PUNCHS);
        gestureCtrl.addGestureHandler(doPunchs06, GesturesActions.DO_PUNCHS);
    }
    
    

    @Override
    public void gestureExecuted(Object userData) {
        GesturesActions actionExecuted = (GesturesActions) userData;
        switch (actionExecuted) {

            case DO_PUNCHS:
                controlledActor.doActionPunch();
                break;
                
            case DO_KICKS:
                controlledActor.doActionKick();
                break;

                
            case DO_JUMP_FORWARD:
                if (!controlledActor.getHorizontalFlip()) {
                    controlledActor.doActionWalkingForward();
                    
                } else {
                    controlledActor.doActionWalkingBackward();
                }
                controlledActor.doActionJump();
                break;
                
            case DO_JUMP_BACKWARD:
                if (controlledActor.getHorizontalFlip()) {
                    controlledActor.doActionWalkingForward();
                    
                } else {
                    controlledActor.doActionWalkingBackward();
                }
                controlledActor.doActionJump();
                break;
                
                
            case DO_JUMP_UP:
                controlledActor.doActionJump();
                break;
                
            case MOVE_FIREBALL_RIGHT:
                if (!controlledActor.getHorizontalFlip()) controlledActor.doActionThrowFireball();
                break;
            case MOVE_FIREBALL_LEFT:
                if (controlledActor.getHorizontalFlip()) controlledActor.doActionThrowFireball();
                break;

            default:
                break;
        }

    }

    public boolean touchDragged(int x, int y, int pointer) {
        gestureCtrl.addMovement(x, y, Gdx.graphics.getDeltaTime());

        return false;
    }

    public boolean touchUp(int x, int y, int pointer, int button) {
        gestureCtrl.finalizeMovement(x, y);
        return false;
    }

    public boolean touchDown(int x, int y, int pointer, int button) {
        gestureCtrl.addMovement(x, y, Gdx.graphics.getDeltaTime());
        return false;
    }

    public void checkForPitchAngle() {

        float pitchAngle = Gdx.input.getPitch();

        if (pitchAngle > 5) {
            if (controlledActor.getHorizontalFlip() == true) {
                controlledActor.doActionWalkingForward();
            } else {
                controlledActor.doActionWalkingBackward();
            }
        }
        if (pitchAngle < -5) {
            if (controlledActor.getHorizontalFlip() == false) {
                controlledActor.doActionWalkingForward();
            } else {
                controlledActor.doActionWalkingBackward();
            }
        }

    }

}
